package com.example.myspringai.volcengine.signer.example;


import com.example.myspringai.util.Base64ToImage;
import com.example.myspringai.volcengine.signer.Credentials;
import com.example.myspringai.volcengine.signer.Signer;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.net.URI;

public class TextToPicExample {

  public static void main(String[] args) throws Exception {
    /* create credentials */
    Credentials credentials = new Credentials();
    credentials.setAccessKeyID("AKLTODY3YzBmYWVlMTA0NDVlMDg3YzIyOGFhZTQ2NDkzY2I");
    credentials.setSecretAccessKey("TlRrelpUQTRNR0l5TmpnMU5HTmpNV0kzTWpNM1kyUXpPVFpqT1RSa05UVQ==");
    credentials.setRegion("cn-north-1");
    credentials.setService("cv");

    /* create signer */
    Signer signer = new Signer();

    /* create http client */
    CloseableHttpClient httpClient = HttpClients.createDefault();

    /* prepare request */
    HttpPost request = new HttpPost();
    request.setURI(new URI("https://visual.volcengineapi.com?Action=HighAesSmartDrawing&Version=2022-08-31"));

    request.addHeader(HttpHeaders.USER_AGENT, "volc-sdk-java/v1.0.0");
    request.addHeader("Content-Type", "application/json");

    // create JSON payload
    JsonObject payload = new JsonObject();
    payload.addProperty("req_key", "high_aes_general_v20");
    payload.addProperty("prompt", "图片风格为3D\n" +
            "\n" +
            "，一个女孩戴着墨镜坐在切好的大片榴莲上，卡通人物设计，圆形拼成的树林，Blender渲染，柔和的灯光" );
    payload.addProperty("model_version", "general_v2.0");

    // set payload to request entity
    StringEntity entity1 = new StringEntity(payload.toString(), HTTP.UTF_8);
    entity1.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    request.setEntity(entity1);

    // sign the request
    signer.sign(request, credentials);

    /* launch request */
    CloseableHttpResponse response = httpClient.execute(request);

    /* status code */
    System.out.println(response.getStatusLine().getStatusCode());   // 200

    /* get response body */
    HttpEntity entity = response.getEntity();
    if (entity != null) {
      String result = EntityUtils.toString(entity);
    // 使用 Gson 解析 JSON 字符串
    Gson gson = new Gson();
    JsonObject jsonObject = JsonParser.parseString(result).getAsJsonObject();

    // 检查是否包含所需的字段
    if (jsonObject.has("data")) {
      JsonObject dataObject = jsonObject.getAsJsonObject("data");
      if (dataObject.has("binary_data_base64")) {
        JsonArray binaryDataArray = dataObject.getAsJsonArray("binary_data_base64");
        // 遍历数组并保存每个图片文件
        for (int i = 0; i < binaryDataArray.size(); i++) {
          String base64String = binaryDataArray.get(i).getAsString();
          boolean success = Base64ToImage.base64ToImage(base64String, "output" + i + ".png");
          if (success) {
            System.out.println("图片" + i + "保存成功");
          } else {
            System.out.println("图片" + i + "保存失败");
          }
        }
      } else {
        System.out.println("binary_data_base64 字段不存在");
      }
    } else {
      System.out.println("data 字段不存在");
    }
    }
    /* close resources */
    response.close();
    httpClient.close();
  }
}
